/**
 * 
 */
package org.tuestudent.grouppa;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;

import android.app.Activity;
import android.app.Fragment;
import android.app.NotificationManager;
import android.content.Context;
import android.text.format.DateFormat;

/**
 * A class for storing current state of the application. Contains information
 * about current user, group etc.
 * 
 * @author s121170
 */
public class AppContext {

    private User currentUser;
    // private GrouppaScreen currentScreen;
    private ViewController currentViewController;
    public Login loginActivity;
    public GrouppaScreen grouppaActivity;
    public String currentUserName;
    public String currentAccessToken;
    public String currentUserPassword;
    public HashMap<String,Group> joinedGroups = new HashMap<String,Group> ();
    public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    /**
     * current screen on which the user sits
     */
    public Fragment currentGrouppaFragment;

    public String[] getJoinedGroupsArray() {
        String[] groupsArray = new String[joinedGroups.size()];
        return joinedGroups.keySet().toArray(groupsArray);
    }

    public enum ActiveScreens {
        login, grouppaScreen
    };

    public ActiveScreens activeScreen = ActiveScreens.login;
    private Group currentGroup=null;

    public Activity getCurrentActivity() {
        switch (activeScreen) {
        case login:
            return loginActivity;
        case grouppaScreen:
            return grouppaActivity;
        }
        return null;
    }

    // private Menu menu;

    public ViewController getCurrentViewController() {
        return currentViewController;
    }

    public void setCurrentViewController(ViewController currentViewController) {
        this.currentViewController = currentViewController;
    }

    public User getCurrentUser() {
        return currentUser;
    }


    /**
     * returns the name of the current group
     * @return
     */
    public String getCurrentGroup() {
        if(currentGroup == null)
            return null;
        return currentGroup.name;
    }

    public String getCurrentGroupPin() {
        if(currentGroup == null)
            return null;
        return currentGroup.pin;
    }
    /**
     * returns true if current user is a manager of current group
     * @return
     */
    public boolean isManager()
    {
        return currentGroup.isManager;
    }

    /**
     * switches active group of the user
     * 
     * @param groupName
     *            groups to switch to
     * @throws Exception
     *             if group doesn't exist
     */
    public void setCurrentGroup(String groupName, boolean clearNotifications) throws Exception {
        if(!joinedGroups.containsKey(groupName))
            throw new Exception("Tried to switch to nonexistent group");
        currentGroup = joinedGroups.get(groupName);
        Main.resetControllers();
        if(clearNotifications)
        {

            NotificationManager mNotificationManager =
                (NotificationManager) getCurrentActivity().getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancelAll();
        }
    }

    /**
     * Call this method after the group was joined on the server to make this
     * change in the app
     * 
     * @param groupName
     *            name of the joined group
     */
    public void addJoinedGroup(String groupName, String password) {
        joinedGroups.put(groupName,new Group(groupName,false,password));
        if (joinedGroupsChangeListener != null) {
        
            joinedGroupsChangeListener
                .onJoinedGroupsChange(getJoinedGroupsArray());
        
        }
    }

    /**
     * Call this method after the group was disconnected from the user on the
     * server to make this change in the app
     * 
     * @param groupName
     *            name of the left group
     */
    public void removeJoinedGroup(String groupName) {
        joinedGroups.remove(groupName);
        if (joinedGroupsChangeListener != null)
            joinedGroupsChangeListener
                    .onJoinedGroupsChange(getJoinedGroupsArray());
    }

    private OnJoinedGroupsChangeListener joinedGroupsChangeListener;

    public void setOnJoinedGroupsChangeListener(
            OnJoinedGroupsChangeListener newListener) {
        joinedGroupsChangeListener = newListener;
    }

    public interface OnJoinedGroupsChangeListener {

        /**
         * Called when content of joined groups list changes
         * 
         * @param newGroups
         */
        public void onJoinedGroupsChange(String[] newGroups);
    }
}
